package com.firebird.app.kotlin.stock.ui.login

import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.Nullable

import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.MainActivity
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.base.BaseActivity
import com.firebird.app.kotlin.stock.base.BaseMvpActivity
import com.firebird.app.kotlin.stock.data.LoginBean
import com.firebird.app.kotlin.stock.data.SimpleBean
import com.firebird.app.kotlin.stock.ui.forgetpwd.ForgetPwdActivity
import com.firebird.app.kotlin.stock.ui.login.contract.LoginContract
import com.firebird.app.kotlin.stock.ui.login.presenter.LoginPresenter
import com.firebird.app.kotlin.stock.ui.regist.RegistActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.MaptoJsonUtils
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import com.firebird.app.kotlin.stock.utils.ToastUtil

import java.io.IOException

import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

class LoginActivity : BaseMvpActivity() , LoginContract.View {
    @JvmField
    @BindView(R.id.et_username)
    var etUsername:EditText?=null
    @BindView(R.id.userpwd)
    var userPwd:EditText ?=null
    @BindView(R.id.tv_forget)
    var tvForget: TextView?=null
    @BindView(R.id.bt_login)
    var  btLogin:TextView?]=null
    @BindView(R.id.bt_regist)
    var  btRegist:TextView?]=null
    private val strUsername:String?=null
    private val strUserpwd:String?=null
    private val dialog:Dialog?=null

    fun getLayoutId():Int? {
        return R.layout.activity_login
    }

    fun initView(){
        mPresenter =  LoginPresenter()
        mPresenter.attachView(this)
        String uname = SharedPreferencesUtil.getInstance(this).getSP("uname");
        if (!TextUtils.isEmpty(uname)) {
            etUsername.setText(uname);
        }
    }
    /*


    @OnClick({R.id.tv_forget, R.id.bt_login, R.id.bt_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_forget:
                //忘记密码
                toClass(this, ForgetPwdActivity.class);
                break;
            case R.id.bt_login:
                //登录
                strUsername = etUsername.getText().toString().trim();
                strUserpwd = userpwd.getText().toString().trim();
                if (TextUtils.isEmpty(strUsername) || TextUtils.isEmpty(strUserpwd)) {
                    ToastUtil.showToast(this, "请输入账号和密码");
                } else {
                    keyList.add("telOrName");
                    keyList.add("loginPwd");
                    valueList.add(strUsername);
                    valueList.add(strUserpwd);
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                }
                break;
            case R.id.bt_regist:
                //注册
                toClass(this, RegistActivity.class, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            etUsername.setText(data.getStringExtra("tel"));
        }
    }

    @Override
    public void onSuccess(LoginBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(this).putSP("token", token);
            SharedPreferencesUtil.getInstance(this).putSP("debtorid", bean.getDebtorId());
            SharedPreferencesUtil.getInstance(this).putSP("uname", strUsername);

            toClass1(this, MainActivity.class);
            finish();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }
    */
}